import router from './router'
import { getAccessToken } from '@/utils/auth'
import { useUserStoreWithOut } from '@/store/modules/user'

const whiteList = [
  '/',
  '/register',
  '/video',
  '/menu',
  '/profile',
  '/search',
  '/forgotPassword',
  '/collection'
]

router.beforeEach(async (to, _from, next) => {
  const userStore = useUserStoreWithOut()

  if (getAccessToken()) {
    // 判断当前用户是否已拉取完登录信息
    if (userStore.userInfoIsEmpty()) {
      await userStore.setUserInfo()
    }
    next()
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/')
    }
  }
})
